package com.sanmu.factory;

import com.sanmu.bean.Monster;
import org.springframework.beans.factory.FactoryBean;

import java.util.HashMap;
import java.util.Map;

/**
 * 是一个factory的bean
 */
public class MyFactoryBean implements FactoryBean<Monster> {
    //这个key就是你配置的时候，指定要获取的对象对应key
    private String key;

    public void setKey(String key) {
        this.key = key;
    }

    private Map<String, Monster> monster_map;

    /*代码块完成初始化任务*/ {
        monster_map = new HashMap<>();
        monster_map.put("monster11", new Monster(10, "小兔子", "小猪"));
        monster_map.put("monster12", new Monster(12, "小龙", "sanmu"));
    }

    @Override
    public Monster getObject() throws Exception {
        return monster_map.get(key);
    }

    @Override
    public Class<?> getObjectType() {

        return Monster.class;
    }

    @Override
    public boolean isSingleton() {
        return true;
    }

}
